package com.xinfu.newproject.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.xinfu.newproject.R;
import com.xinfu.newproject.base.BaseActivity;
import com.xinfu.newproject.common.SDActivityManager;
import com.xinfu.newproject.ui.HomeFragment;
import com.xinfu.newproject.ui.MyFragment;
import com.xinfu.newproject.ui.ServiceFragment;
import com.xinfu.newproject.util.BToast;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomeActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.home_tab_life)
    RelativeLayout tabOne;

    @BindView(R.id.home_tab_service)
    RelativeLayout tabTwo;

    @BindView(R.id.home_tab_my_rl)
    RelativeLayout tabThree;

    @BindView(R.id.home_tab_life_iv)
    ImageView tabIv1;

    @BindView(R.id.home_tab_life_tv)
    TextView tabTv1;

    @BindView(R.id.home_tab_service_iv)
    ImageView tabIv2;

    @BindView(R.id.home_tab_service_tv)
    TextView tabTv2;

    @BindView(R.id.home_tab_my_iv)
    ImageView tabIv3;

    @BindView(R.id.home_tab_my_tv)
    TextView tabTv3;

    private HomeFragment homeFragment;
    private ServiceFragment serviceFragment;
    private MyFragment myFragment;
    private FragmentManager fragmentManager;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
        initViewBind();
        initLisener();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (fragmentManager != null) {

        } else {
            fragmentManager = getSupportFragmentManager();
        }
    }

    private void initLisener() {
        tabOne.setOnClickListener(this);
        tabTwo.setOnClickListener(this);
        tabThree.setOnClickListener(this);
    }

    private void initViewBind() {
        tabIv1.setImageResource(R.mipmap.tab_life_pressed);
        tabTv1.setTextColor(getResources().getColor(R.color.yellow_ff));

        homeFragment = new HomeFragment();
        fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .add(R.id.home_replace_frameLayout, homeFragment)
                .show(homeFragment)
                .commit();
    }

    @Override
    public void onClick(View view) {
        FragmentTransaction beginTransaction =
                fragmentManager.beginTransaction();
        switch (view.getId()) {
            case R.id.home_tab_life:
                restoreNormal();
                tabIv1.setImageResource(R.mipmap.tab_life_pressed);
                tabTv1.setTextColor(getResources().getColor(R.color.yellow_ff));
                hideFragments(beginTransaction);
                if (homeFragment != null) {
                    beginTransaction.show(homeFragment);
                } else {
                    homeFragment = new HomeFragment();
                    beginTransaction.add(R.id.home_replace_frameLayout, homeFragment, "homeOne")
                            .show(homeFragment);
                }
                break;
            case R.id.home_tab_service:
                restoreNormal();
                tabIv2.setImageResource(R.mipmap.tab_service_pressed);
                tabTv2.setTextColor(getResources().getColor(R.color.yellow_ff));
                hideFragments(beginTransaction);
                if (serviceFragment != null) {
                    beginTransaction.show(serviceFragment);
                } else {
                    serviceFragment = new ServiceFragment();
                    beginTransaction.add(R.id.home_replace_frameLayout, serviceFragment, "homeTwo")
                            .show(serviceFragment);
                }
                break;
            case R.id.home_tab_my_rl:
                restoreNormal();
                tabIv3.setImageResource(R.mipmap.tab_my_pressed);
                tabTv3.setTextColor(getResources().getColor(R.color.yellow_ff));
                hideFragments(beginTransaction);
                if (myFragment != null) {
                    beginTransaction.show(myFragment);
                } else {
                    myFragment = new MyFragment();
                    beginTransaction.add(R.id.home_replace_frameLayout, myFragment, "homeThree")
                            .show(myFragment);
                }
                break;
        }

        beginTransaction.commit();
    }

    /**
     * 隐藏fragment
     *
     * @param transaction
     */
    private void hideFragments(FragmentTransaction transaction) {

        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (serviceFragment != null) {
            transaction.hide(serviceFragment);
        }
        if (myFragment != null) {
            transaction.hide(myFragment);
        }
    }

    /**
     * 恢复按钮和字体颜色
     */
    private void restoreNormal() {
        tabIv1.setImageResource(R.mipmap.tab_life_normal);
        tabIv2.setImageResource(R.mipmap.tab_service_normal);
        tabIv3.setImageResource(R.mipmap.tab_my_normal);


        tabTv1.setTextColor(getResources().getColor(R.color.tabcolor_normal));
        tabTv2.setTextColor(getResources().getColor(R.color.tabcolor_normal));
        tabTv3.setTextColor(getResources().getColor(R.color.tabcolor_normal));

    }

    //解决重叠，方法1
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //如果用以下这种做法则不保存状态，再次进来的话会显示默认的tab
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        Log.i("testOne","requestCode="+requestCode+"resultCode="+resultCode);

    }


    //退出app
    private long mExitTime;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitApp();
        }

        return true;
    }

    public void exitApp() {
        if (System.currentTimeMillis() - mExitTime > 1200) {

//            new AlertDialog.Builder(this).setTitle("提示：")
//                    .setMessage("确定退出应用吗？")
//                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
//                            dialog.dismiss();
//                        }
//                    }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialog, int which) {

//                    dialog.dismiss();

//                }
//            }).show();
            BToast.showText(this, "再按一次退出应用");

        } else {
            //销毁activity
            SDActivityManager.getInstance().finishAllActivity();
            System.exit(0);
        }
        mExitTime = System.currentTimeMillis();
    }

}
